<template>
  <div class="qrcode-login-mobile">
    <div
      v-if="!isJialianEnter"
      class="bg"
    />
    <div
      v-else
      class="bg-jialian"
    />
    <div class="choose-content">
      <div class="title">
        选择账号登录
      </div>
      <div
        v-loading="listLoading"
        class="account-list"
      >
        <div
          v-for="(merchant, merchantIndex) in accounts"
          :key="merchantIndex"
          class="merchant-wrapper"
        >
          <div
            v-for="(insItem, insIndex) in merchant.ins || []"
            :key="insIndex"
            class="ins-wrapper"
          >
            <div 
              v-for="(accountItem, accountIndex) in insItem.accounts" 
              :key="accountIndex" 
              class="accounts-wrapper"
              @click="selectAccount(accountItem, insItem)"
            >
              <div
                class="account-name"
                :class="{ 'account-name__active': accountItem.account === selectedAccount.account }"
              >
                {{ insItem.name }}--{{ accountItem.name || accountItem.account || insItem.name || '暂无账号名称' }}
              </div>
              <img
                src="@/assets/login/choose.png"
                alt=""
                srcset=""
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      v-if="dialogVisible"
      class="overlay-moni"
    />
    <div
      v-if="dialogVisible"
      class="dialog-moni"
    >
      <h1>温馨提示</h1>
      <h3>[{{ selectedIns.name }}-{{ selectedAccount.name || selectedAccount.account || '暂无账号名称' }}]</h3>
      <p>确认登录此账号？</p>
      <div class="button-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button
          v-loading="loginLoading"
          :disabled="isLoginBtnDisable"
          class="blue"
          @click="login"
        >
          登录
        </el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { loginByQRCode, selectAccounts } from "@/api/login";
import { ElMessage } from "element-plus";
import vconsole from "vconsole";
import { IAccMerchant, IAccount } from "@/api/modules/login";

const route = useRoute()
const router = useRouter()
const dialogVisible = ref(false)
const certificate = ref<string>('')
const accounts = ref<IAccMerchant[]>([])
const listLoading = ref(false)
const getAccountsList = async () => {
  const tag = route.query.tag as string ?? ''
  const wxCode = route.query.code as string ?? ''
  listLoading.value = true
  const rs = await selectAccounts({
    tag,
    wxCode,
    app: 'merchant'
  })
  listLoading.value = false
  if (rs.code === 200) {
    certificate.value = rs.payload?.certificate ?? ''
    accounts.value = rs.payload?.merchantAccounts ?? []
  } else {
    ElMessage.error(rs?.errorMsg ?? '请重新扫码')
  }
}
const selectedAccount = ref<IAccount>({} as IAccount)
const selectedIns = ref({})
const selectAccount = (accountItem: IAccount, insItem: any) => {
  selectedAccount.value = accountItem
  selectedIns.value = insItem
  dialogVisible.value = true
}

const loginLoading = ref(false)
const isLoginBtnDisable = ref(false)
const login = async () => {
  loginLoading.value = true
  const rs = await loginByQRCode({
    certificate: certificate.value,
    account: selectedAccount.value.account
  })
  loginLoading.value = false

  if (rs.code === 200) {
    ElMessage.success({
      message: '登录成功，可以关闭此页面',
      duration: 0,
    })
    isLoginBtnDisable.value = true
    dialogVisible.value = false
  }
}

const getQuery = () => {
  return new Promise<void>((resolve) => {
    const authUrlQueryString = (location.href.match(/\?.+(?=#)/i)?.[0] ?? '').replace('?', '')
    const newHref = location.href.replace('?' + authUrlQueryString, '')

    if (authUrlQueryString) {
      location.replace(newHref + '&' + authUrlQueryString)
    } else {
      resolve()
    }
  })

}

const isJialianEnter = ref(false);

// 判断当前路由
const beforeRouteEnter = () => {
  if (window.location.href.indexOf('yxqy.jlpay.com') != -1) {
    isJialianEnter.value = true;
    // console.log('panduan 当前路由1')
  } else {
    isJialianEnter.value = false;
    // console.log('panduan 当前路由2')
  }
}

onBeforeMount(() => {
  beforeRouteEnter();
  getQuery().then(() => {
    getAccountsList()
  })
})

onMounted(() => {
  new vconsole()
})

</script>

<style scoped lang="scss">
.qrcode-login-mobile {
  max-width: 450px;
  margin: 0 auto;
  padding-bottom: 100px;

  .bg {
    background: url("@/assets/login/bg.jpg") no-repeat no-repeat center center;
    background-size: 100% 100%;
    height: 289px;
  }

  .bg-jialian {
    background: url("@/assets/login/bg-jialian.png") no-repeat no-repeat center center;
    background-size: 100% 100%;
    height: 289px;
  }

  .choose-content {
    margin-top: -40px;
    background: #FFFFFF;
    box-shadow: 0px -6px 18px 0px rgba(75, 147, 255, 0.08);
    border-radius: 40px 40px 0px 0px;
    padding: 30px;
    box-sizing: border-box;
  }

  .title {

    font-size: 18px;
    font-weight: bold;
  }
}

.account-list {
  margin-top: 15px;

  .merchant-wrapper {
    margin-bottom: 10px;

    .merchant-name {
      font-size: 16px;
      color: #333;
      padding: 10px;
    }
  }

  .ins-wrapper {
    margin-bottom: 5px;

    .ins-name {
      font-size: 16px;
      color: #666;
      padding: 10px 0;
      margin: 0 20px;
    }
  }

  .accounts-wrapper {
    height: 49px;
    background: #F0F3FA;
    border-radius: 24px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-bottom: 20px;

    .account-name {
      width: 222px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
      font-size: 17px;
      color: #999;
      padding: 10px;
      margin: 0 10px;
      border-bottom: solid 1px #eee;
      transition: all ease-in 300ms;
    }

    img {
      width: 46px;
      height: 46px;
    }

    // .account-name__active {
    //   background: rgba(232, 242, 255, 0.5000);
    //   color: var(--el-color-primary);
    //   border-radius: 5px;
    //   border-bottom: solid 1px transparent;

    // }
  }
}

.fixed-button {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 10px 10px 10px;
  padding-bottom: calc(10px + constant(safe-area-inset-bottom));
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  box-sizing: border-box;
  background: white;

  :deep(.el-button) {
    width: 100%;
  }
}

.overlay-moni {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  background-color: rgba(0, 0, 0, .7);
}

.dialog-moni {
  width: 270px;
  height: 160px;
  background: #FFFFFF;
  border-radius: 14px;
  position: fixed;
  z-index: 150;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding-top: 20px;
  box-sizing: border-box;

  h1 {
    width: 100%;
    text-align: center;
    font-size: 18px;
    color: #000000;
    line-height: 18px;
    margin-bottom: 12px;
  }

  h3 {
    width: 100%;
    text-align: center;
    font-size: 15px;
    color: #505052;
    line-height: 15px;
    margin-bottom: 10px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  p {
    width: 100%;
    text-align: center;
    font-size: 15px;
    color: #888888;
    line-height: 15px;
    margin-bottom: 5px;
  }

  .button-footer {
    position: absolute;
    bottom: 0;
    left: 0;
    display: flex;
    width: 100%;
    height: 50px;
    overflow: hidden;

    :deep(.el-button) {
      height: 50px;
      width: 50%;
      border: 0;
      border-top: 1px solid #DCDEE3;
      border-right: 1px solid #DCDEE3;
      margin: 0 !important;
      color: #000000;
    }

    .blue {
      color: #1964FA;
    }
  }
}</style>
